;+
; NAME:
;  hod_plotpars
; PURPOSE:
;  Makes plots of posterior constraints on parameters for an MCMC run
; CALLING SEQUENCE:
;  hod_plotpars,savfile,[/zheng,outfile=outfile]
; INPUTS:
;  savfile - name of IDL save file containing MCMC results
; OPTIONAL KEYWORDS:
;  /zheng - overplot the Zheng et al. 2007 results
;
; HISTORY:
;
;       Tue May 15 01:32:51 2012, <jaird@offshore.ucsd.edu>
;
;		 outfile -
;output plot ps file -
pro hod_plotpars,savfile,zheng=zheng,outfile=outfile,nburn=nburn_in,nsamp=nsamp_in,title=title

   if n_elements(savfile) eq 0 then begin
       splog,"Must specify savfile"
       return
   endif

   restore,savfile
   if n_elements(nburn_in) gt 0 then begin
       nsamp=nsamp+nburn-nburn_in
       nburn=nburn_in
   endif
   if n_elements(nsamp_in) gt 0 then nsamp=nsamp_in

   if n_elements(outfile) gt 0 then $
     ja_plotconfig,outfile=outfile,xsize=20,ysize=20,charsize=0.5

   zhengpar=[11.83,0.30,11.53,13.02,0.97]
   zhengerrhi=[0.08,0.18,0.65,0.11,0.11]
   zhengerrlo=[0.07,0.19,0.65,0.10,0.11]

   parname=['$\log M_\mathrm{min}$','$\sigma_{\log M}$','$\log M_0$','$\log M^\prime_1$','$\alpha$']

;   hod_priorspec[*,0]=[11.5,12.5]
;   hod_priorspec[*,3]=[12,13.5]

   !p.multi=[0,hod_npar-1,hod_npar-1,0,1]
   !x.margin=[8,8]
   !y.margin=[3.5,5]
   multiplot
   erase

   ; recalculating parmean and parerror
   for i=0,n_elements(parmean)-1 do begin
      parmean[i]=mean(samps[i,nburn:nburn+nsamp-1])
      parerror[i]=stddev(samps[i,nburn:nburn+nsamp-1])
   endfor

   
   for i1=0,hod_npar-2 do begin
       for i2=0,hod_npar-2 do begin
           if i1 eq 0 then ytit=parname[i2] else ytit=' '
           if i2 eq hod_npar-2 then xtit=parname[i1] else xtit=' '
           if i2 eq 0 and i1 eq (hod_npar-1)/2 and n_elements(title) gt 0 then tit=title else tit=' '
           if i2 eq hod_npar-2 then !x.tickname=strarr(10)+'' else !x.tickname=strarr(10)+' '
           if i1 eq i2 then begin
               s=samps[i1,nburn:nsamp+nburn-1]
               plothist,s,/autobin,xrange=hod_priorspec[*,i1],xhist,yhist,peak=1.0,/noplot
               ja_plot,xhist,yhist*(hod_priorspec[1,i1]-hod_priorspec[0,i1])+hod_priorspec[0,i1],$
                 xstyle=3,ystyle=3,/yno,psym=10,xrange=hod_priorspec[*,i1],yrange=hod_priorspec[*,i1],$
                 xtitle=xtit,ytitle=ytit,title=tit;,xticks=3,yticks=3
           endif else begin
               ja_plot,[0],[0],xrange=hod_priorspec[*,i1],yrange=hod_priorspec[*,i2],$
                 xtitle=xtit,ytitle=ytit,title=tit,xstyle=3,ystyle=3;,xticks=3,yticks=3
               hogg_scatterplot,samps[i1,nburn:nsamp+nburn-1],samps[i2,nburn:nsamp+nburn-1],$
                 /nogrey,levels=1-(erfc([1.0,2.0])),xnpix=30,ynpix=30,$;xticks=3,yticks=3,$
                 xrange=hod_priorspec[*,i1],yrange=hod_priorspec[*,i2],xstyle=3,ystyle=3,/noerase
               ;djs_oplot,[parmean[i1]],[parmean[i2]],psym=7,col='red'
           endelse
           
           oploterror,parmean[i1],parmean[i2],parerror[i1],parerror[i2],psym=3,col=fsc_color('Orange Red',101),errcol=fsc_color('Orange Red',101),/nohat
           if keyword_set(zheng) then begin
               oploterror,zhengpar[i1],zhengpar[i2],zhengerrhi[i1],zhengerrhi[i2],/hibar,psym=3,$
                 col=fsc_color('Dodger Blue',100),errcol=fsc_color('Dodger Blue',100),/nohat
               oploterror,zhengpar[i1],zhengpar[i2],zhengerrlo[i1],zhengerrlo[i2],/lobar,psym=3,$
                 col=fsc_color('Dodger Blue',100),errcol=fsc_color('Dodger Blue',100),/nohat
           endif
               

           multiplot
       endfor       
   endfor
           
   
   
   cleanplot,/silent

   if n_elements(outfile) gt 0 then ja_plotconfig,/close
end

